package cn.itcast.tanhua.mytanhuaserver.controller;

import cn.itcast.tanhua.mytanhuaserver.service.PeachService;
import cn.itcast.tanhua.vo.SoundVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 张钊煌
 * @since 2021/12/6 16:33
 */
@RestController
@RequestMapping("/peachblossom")
public class PeachblossomController {
    @Autowired
    private PeachService peachService;

    @GetMapping
    public ResponseEntity<SoundVo> receivePeach(){
        try {
            SoundVo soundVo= peachService.receivePeach();
            if (soundVo!=null){
                return ResponseEntity.ok(soundVo);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping()
    public ResponseEntity<Object> sendPeach(@RequestParam("soundFile") MultipartFile soundFile){
        System.out.println(soundFile.getName());
        try {
            Boolean bool=peachService.sendPeach(soundFile);
            if (bool){
                return ResponseEntity.ok(null);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
